package com.mayisoftware.mayioa.business.plan.impl.controller;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanCustom;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanCustomService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/plan/evaluation")
public class PlanCustomController extends BaseController{
    private String prefix = "plan/evaluation";

    @Autowired
    IPlanCustomService planCusService;

    @RequiresPermissions("plan:evaluation:view")
    @GetMapping()
    public String evaluation()
    {
        return prefix + "/evaluation";
    }

    /**
     * 查看评价页面
     * @param planCustom
     * @return
     */
    @RequiresPermissions("plan:evaluation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlanCustom planCustom){
        startPage();
        List<PlanCustom> list = planCusService.selectPlanCusList(planCustom);
        return getDataTable(list);
    }

    /**
     * 新增评价页面
     * @param mmap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增评价保存
     * @param planCustom
     * @return
     */
    @RequiresPermissions("plan:evaluation:add")
    @Log(title = "计划评价管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(PlanCustom planCustom){
        return toAjax(planCusService.insertPlanCus(planCustom));
    }

    /**
     * 修改评价
     */
    @GetMapping("/edit/{planCusId}")
    public String edit(@PathVariable("planCusId") Long planCusId, ModelMap mmap)
    {
        mmap.put("planCustom", planCusService.selectPlanCusById(planCusId));
        return prefix + "/edit";
    }

    @RequiresPermissions("plan:evaluation:edit")
    @Log(title = "计划评价管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(PlanCustom planCustom){
        planCustom.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(planCusService.updatePlanCus(planCustom));
    }

    @RequiresPermissions("plan:evaluation:remove")
    @Log(title = "计划评价管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try{
            return toAjax(planCusService.deletePlanCusIds(ids));
        }catch (Exception e){
            return toAjax(0);
        }
    }


}
